<template>
  <Table
    ref="tableRef"
    :url="URL_PAGINGTASK"
    :columns="data.columns"
    :handles="data.handles"
    :query="data.query"
  ></Table>
</template>


<script lang="ts" setup name="couponRecordList">
import Table from '@/components/table/index.vue';
import { EXECUTE_TYPE } from '@/utils/dictionaries';
import { queryDicByParent } from '@/service/apis/api';
import { URL_PAGINGTASK, terminateTask } from '@/service/apis/market';

const router = useRouter();

const tableRef = ref();

const emit = defineEmits(['get-update']);
onActivated(() => {
  emit('get-update', (flag: boolean) => {
    if (flag) {
      tableRef.value?.getData();
    }
  });
});

queryDicByParent({
  listDtos: ['couponTaskStatus', 'couponTaskReason'],
}).then((res: any) => {
  if (res) {
    data.columns[2].dic = [...res.couponTaskStatus];
    data.columns[3].dic = [...res.couponTaskReason];
    data.query[2].props.options = [...res.couponTaskStatus];
    data.query[3].props.options = [...res.couponTaskReason];
  }
});

const data = reactive({
  columns: [
    {
      prop: 'name',
      label: '任务名称',
    },
    {
      prop: 'executeType',
      label: '发放模式',
      dic: EXECUTE_TYPE,
    },
    {
      prop: 'status',
      label: '任务状态',
      dic: [] as any,
      type: Number,
    },
    {
      prop: 'reason',
      label: '终止原因',
      dic: [] as any,
      type: Number,
    },
    {
      prop: 'updateBy',
      label: '修改人',
    },
    {
      prop: 'updateTime',
      label: '修改时间',
    },
  ],
  handles: [
    {
      label: '详情',
      handle: (row: any) => {
        console.log('详情 ---', row);
        router.push({
          name: 'couponRecordDetail',
          query: { id: row.id },
        });
      },
    },
    {
      label: '修改',
      permission: 'couponRecordList.edit',
      disabledFun: (row: any) => {
        return row.status !== 0;
      },
      handle: (row: any) => {
        console.log('编辑---', row);
        router.push({
          name: 'couponTaskEdit',
          query: { id: row.id },
        });
      },
    },
    {
      label: '停止',
      permission: 'couponRecordList.stop',
      disabledFun: (row: any) => {
        return row.status !== 0 && row.status !== 1;
      },
      handle: (row: any) => {
        console.log('停止---', row);
        ElMessageBox.confirm(
          '此操作将停止当前任务，任务停止后不再发放优惠券，是否继续？',
          '提示',
          {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          }
        ).then(() => {
          terminateTask(Number(row.id)).then((res: any) => {
            tableRef.value.getData();
          });
        });
      },
    },
  ],
  query: [
    {
      label: '任务名称',
      prop: 'name',
      comName: 'input',
      props: {
        placeholder: '请输入任务名称',
      },
    },
    {
      label: '发放模式',
      prop: 'executeType',
      comName: 'select',
      props: {
        placeholder: '请选择发放模式',
        change: (value: any) => {
          console.log(value);
        },
        options: EXECUTE_TYPE,
      },
    },
    {
      label: '任务状态',
      prop: 'status',
      comName: 'select',
      props: {
        placeholder: '请选择任务状态',
        change: (value: any) => {
          console.log(value);
        },
        options: [] as any,
      },
    },
    {
      label: '终止原因',
      prop: 'reason',
      comName: 'select',
      props: {
        placeholder: '请选择终止原因',
        change: (value: any) => {
          console.log(value);
        },
        options: [] as any,
      },
    },
  ],
});
</script>